﻿@using ApproveFlowEngine.Approve
@using ApproveFlowEngine.Approve.DTO
@using ApproveFlowEngine.Enums.Core

<Collapse>
    @foreach (var item in Nodes)
    {
        var isLeaf = item.NodeType is NodeType.Simple;
        Console.WriteLine(item.NodeType);
        <Panel Key=@item.Id.ToString()>
            <ExtraTemplate>
                @{

                    if (item.Activity)
                    {
                        <Tag Color="green">@nameof(item.Activity)</Tag>
                        <Tag Color="green">@item.Status</Tag>
                    }
                    else
                    {
                        <Tag Color="volcano">@item.Status</Tag>
                    }

                    if (IsEdit)
                    {
                        if (item.ParentId is not null)
                        {
                            <Button @onclick="c=>DeleteNode(Nodes,item)">Delete</Button>
                        }


                        if (!isLeaf)
                        {
                            <Dropdown Placement="@Placement.Bottom">
                                <Overlay>
                                    @foreach (var nodeTypeItem in _nodeTypes)
                                    {
                                        <MenuItem>
                                            <Button @onclick="c=>AddNode(item.Children,nodeTypeItem.Key)">Add @nodeTypeItem.Value Node</Button>
                                        </MenuItem>
                                    }
                                </Overlay>
                                <ChildContent>
                                    <Button>Add Node</Button>
                                </ChildContent>
                            </Dropdown>
                        }
                        else
                        {


                            if (item.Activity)
                            {
                                <Button>Approve</Button>
                            }
                        }


                    }

                }
            </ExtraTemplate>
            <HeaderTemplate>
                @item.NodeType.ToString()
            </HeaderTemplate>
            <ChildContent>

                @if (!isLeaf)
                {
                    <ApproveTree Nodes=@item.Children IsEdit="IsEdit"></ApproveTree>
                }
                else
                {

                    <label>ApproverId</label> <Input @bind-Value=@item.ApproverId></Input>

                }
            </ChildContent>
        </Panel>
    }
</Collapse>


@code {
    [Parameter]
    public List<WorkNodeDto> Nodes { get; set; } = new List<WorkNodeDto>();
    [Parameter]
    public bool IsEdit { get; set; }

    void DeleteNode(List<WorkNodeDto> nodes, WorkNodeDto node)
    {

        nodes.Remove(node);

    }
    void AddNode(List<WorkNodeDto> nodes, NodeType type)
    {
        nodes.Add(new WorkNodeDto()
            {
                Children = new List<WorkNodeDto>(),
                Index = nodes.Count + 1,
                NodeType = type,
                ApproverId = null
            });
    }
    private Dictionary<NodeType, string> _nodeTypes = new Dictionary<NodeType, string>();

    protected override void OnInitialized()
    {
        var types = Enum.GetValues<NodeType>();
        foreach (var item in types)
        {
            _nodeTypes.Add(item, item.ToString());
        }
    }
}
